﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace Insesoft.UI.Reports
{
    public partial class ReportViewerForm : Form
    {
        private ReportDataSource reportDataSource;

        public ReportViewerForm()
        {
            InitializeComponent();
        }

        public string ReportTitle
        {
            set
            {
                Text = value;
            }
        }

        private void ReportViewerFormLoad(object sender, EventArgs e)
        {
            reportViewer1.RefreshReport();
        }

        private void ShowReport(Dictionary<string, object> dataSources, string reportType, IEnumerable<ReportParameter> parameters)
        {
            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.SetDisplayMode(DisplayMode.PrintLayout); 
            foreach (var source in dataSources)
            {
                reportDataSource = new ReportDataSource(source.Key, source.Value);
                reportViewer1.LocalReport.DataSources.Add(reportDataSource);
            }
            reportViewer1.LocalReport.ReportEmbeddedResource = reportType;
            if (parameters != null)
                reportViewer1.LocalReport.SetParameters(parameters);
            reportViewer1.RefreshReport();
        }

        public void BuildReport(IEnumerable data, string dataType, string reportType, IEnumerable<ReportParameter> parameters)
        {
            var dataSources = new Dictionary<string, object> {{dataType, data}};
            ShowReport(dataSources, reportType, parameters);
        }
       
    }
}
